package com.palm.hubo2o.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.palm.hubo2o.R;
import com.palm.hubo2o.event.IssueHistoryEvent;
import com.palm.hubo2o.util.EnumCode;
import com.palm.hubo2o.view.PrizeView;

import de.greenrobot.event.EventBus;


/**
 * Created by dest1 on 2015/10/10.
 */
public class PrizeAdapter extends RecyclerView.Adapter<PrizeAdapter.PrizeHolder> {
    private EnumCode.gameId[] lotteries = {EnumCode.gameId.SSQ, EnumCode.gameId.QLC, EnumCode.gameId.D3, EnumCode.gameId.HBP3};


    @Override
    public PrizeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.widget_prize_view, parent, false);
        PrizeHolder h = new PrizeHolder(v);
//        h.balls = BallUtils.createBallGroup(h.ballGroup, getGameId(viewType));
        return h;
    }

    @Override
    public void onBindViewHolder(PrizeHolder holder, int position) {
        setIssueInfo(holder, getGameId(position));
    }

    private void setIssueInfo(PrizeHolder holder, EnumCode.gameId lotteryId) {
//        holder.lotteryName.setText(lotteryId.getName());
//        List<Issue> l = IssueCenter.open().switchIssueList(lotteryId);
//        if (!l.isEmpty()) {
//            Issue issue = l.get(0);
//            holder.lotteryIssue.setText(String.format(Locale.getDefault(), "第%d期", issue.issueNo));
//            holder.lotteryDate.setText(TimeUtils.formatTime(TimeUtils.DEFAULT_FULL_FORMAT, issue.prizeTime));
////            String[] balls = BallUtils.analyzeBalls(issue.prizeBalls, lotteryId);
////            for (int i = 0; i < holder.balls.size(); i++) {
////                holder.balls.get(i).setNumber(balls[i]);
////            }
//            List<String> balls = issue.prizeBalls.getRed();
//            if (balls != null) {
//                if (issue.prizeBalls.getBlue() != null)
//                    balls.addAll(issue.prizeBalls.getBlue());
//                for (int i = 0; i < Math.min(holder.balls.size(), balls.size()); i++) {
//                    holder.balls.get(i).setNumber(balls.get(i));
//                }
//            }
//        } else {
//            holder.lotteryIssue.setText("正在加载");
//            IssueCenter.open().startLoadHistory(lotteryId, true);
//        }
    }

    private EnumCode.gameId getGameId(int position) {
        EnumCode.gameId gameId = null;
        switch (position) {
            case 0:
                gameId = EnumCode.gameId.SSQ;
                break;
            case 1:
                gameId = EnumCode.gameId.QLC;
                break;
            case 2:
                gameId = EnumCode.gameId.D3;
                break;
            case 3:
                gameId = EnumCode.gameId.HBP3;
                break;
        }
        return gameId;
    }

    private int getGamePosition(EnumCode.gameId gameId) {
        int pos;
        switch (gameId) {
            case SSQ:
                pos = 0;
                break;
            case QLC:
                pos = 1;
                break;
            case D3:
                pos = 2;
                break;
            case HBP3:
                pos = 3;
                break;
//            case C522:
//                pos = 4;
//                break;
            default:
                pos = -1;
                break;
        }
        return pos;
    }

    @Override
    public int getItemCount() {
        return lotteries.length;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public void onEventMainThread(IssueHistoryEvent event) {
        int p = getGamePosition(event.lotteryId);
        if (p != -1) {
            notifyItemChanged(p);
        }
    }

    public static class PrizeHolder extends RecyclerView.ViewHolder {
        public PrizeView prizeView;

        public PrizeHolder(View view) {
            super(view);
            prizeView = (PrizeView) itemView;
        }


    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        EventBus.getDefault().register(this);
    }
}
